package com.jeeplus.test.jlnu.admissionInfo.utils;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.jeeplus.test.jlnu.admissionInfo.domain.entity.ContinueEdu;
import com.jeeplus.test.jlnu.admissionInfo.domain.entity.RegistrarOffice;
import com.jeeplus.test.jlnu.admissionInfo.domain.entity.StudentInfoEntity;
import com.jeeplus.test.jlnu.admissionInfo.service.ExcelService;
import com.jeeplus.test.jlnu.admissionInfo.service.ExcelServiceStu;
import com.jeeplus.test.postgraduate.domain.vo.PostgraduateReqVo;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class ExcelDataListener extends AnalysisEventListener<RegistrarOffice> {

    private final ExcelService excelService;
    private final ExcelServiceStu excelServiceStu;
    private final ProgressCallback progressCallback; // 用于更新进度
    private final String officeId;
    private static final int BATCH_COUNT = 500; // 每500条记录插入一次数据库

    private List<RegistrarOffice> dataList = new ArrayList<>();
    private List<PostgraduateReqVo> dataListStu = new ArrayList<>();
    private List<ContinueEdu> dataListCon = new ArrayList<>();
    private int totalRecords; // 总记录数
    private int currentRecords; // 当前已处理记录数
    private int type;
    private final String importType;
    private final String templateInfoType;

    // 通过构造函数传入 ExcelService 和进度回调
    public ExcelDataListener(ExcelService excelService, ProgressCallback progressCallback, int totalRecords, int type, ExcelServiceStu excelServiceStu, String officeId1,String importType,String templateInfoType) {
        this.excelService = excelService;
        this.progressCallback = progressCallback;
        this.totalRecords = totalRecords;
        this.type = type;
        this.excelServiceStu = excelServiceStu;
        this.officeId = officeId1;
        this.importType = importType;
        this.templateInfoType = templateInfoType;
    }

    @Override
    public void invoke(RegistrarOffice data, AnalysisContext context) {
        // 获取当前行号
        int currentRow = context.readRowHolder().getRowIndex();

        // 判断当前是否是第二行，跳过第二行
        if (currentRow == 1) {
            return; // 跳过第二行
        }

        data.setOfficeId(officeId);
        dataList.add(data);
        currentRecords++;

        // 当数据量达到批次大小时，保存到数据库
        if (dataList.size() >= BATCH_COUNT) {
            saveDataToDatabase();
            dataList.clear();  // 保存后清空列表，准备接收新一批数据
        }

        // 更新进度
        updateProgress();
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        // 保存剩余数据
        if (!dataList.isEmpty()) {
            if(this.type==1){
                saveDataToDatabase();
            }else if(this.type==2){

            }else {
                if(!dataListStu.isEmpty()){
                    saveDataToDatabaseStu();
                }
            }

            dataList.clear();  // 清空剩余数据
        }

        // 处理完成后更新进度
        updateProgress();
    }

    private void saveDataToDatabase() {
        if (!dataList.isEmpty()) {
            excelService.insertData(dataList); // 保存数据
        }
    }
    private void saveDataToDatabaseStu() {
        if (!dataListStu.isEmpty()) {
            excelServiceStu.insertDataStu(dataListStu,importType,templateInfoType,officeId,631); // 保存数据
        }
    }

    private void updateProgress() {
        int progress = (int) (((double) currentRecords / totalRecords) * 100);
        progressCallback.onProgress(progress);
    }

    // 进度更新回调接口
    public interface ProgressCallback {
        void onProgress(int progress);
    }
}
